using System;
using System.Collections.Generic;
using BitMango;
using BitMango.Diagnostics;
using JsonFx.Json;
using UnityEngine;

internal class PRequest
{
	public static void Initialise(string publickey, string privatekey, string apiurl)
	{
		if (!apiurl.EndsWith("/"))
		{
			apiurl += "/";
		}
		PRequest.APIURL = apiurl + "v1?publickey=" + publickey;
		PRequest.PRIVATEKEY = privatekey;
		PRequest.PUBLICKEY = publickey;
	}

	public static WWW Prepare(string section, string action, Dictionary<string, object> postdata = null)
	{
		if (postdata == null)
		{
			postdata = new Dictionary<string, object>();
		}
		else
		{
			postdata.Remove("publickey");
			postdata.Remove("section");
			postdata.Remove("action");
		}
		postdata.Add("publickey", PRequest.PUBLICKEY);
		postdata.Add("section", section);
		postdata.Add("action", action);
		string text = JsonWriter.Serialize(postdata);
		WWWForm wwwform = new WWWForm();
		wwwform.AddField("data", PEncode.Base64(text));
		wwwform.AddField("hash", PEncode.MD5(text + PRequest.PRIVATEKEY));
		return new WWW(PRequest.APIURL, wwwform);
	}

	public static PResponse Process(WWW www)
	{
		if (Platform.IsEU())
		{
			return PResponse.Error(1);
		}
		if (www == null)
		{
			return PResponse.GeneralError(1);
		}
		if (www.error != null)
		{
			return PResponse.GeneralError(www.error);
		}
		if (string.IsNullOrEmpty(www.text))
		{
			return PResponse.Error(1);
		}
		PResponse result;
		try
		{
			Dictionary<string, object> dictionary = JsonReader.Deserialize<Dictionary<string, object>>(www.text);
			if (!dictionary.ContainsKey("success") || !dictionary.ContainsKey("errorcode"))
			{
				result = PResponse.GeneralError(1);
			}
			else
			{
				result = new PResponse
				{
					success = (bool)dictionary["success"],
					errorcode = (int)dictionary["errorcode"],
					json = dictionary
				};
			}
		}
		catch (Exception ex)
		{
			Debugger.Assert(false, "PRequest.Process json decode error: " + www.text + "\n Exception:" + ex.ToString());
			result = PResponse.GeneralError(1);
		}
		return result;
	}

	public static Dictionary<string, PResponse> Requests = new Dictionary<string, PResponse>();

	private static string APIURL;

	public static string PUBLICKEY;

	public static string PRIVATEKEY;
}
